package com.aiit.controller;

import com.aiit.pojo.bo.Task;
import com.aiit.pojo.bo.User;
import com.aiit.pojo.vo.UserStatus;
import com.aiit.service.TaskManageService;
import com.aiit.util.DateFormatUtil;
import com.aiit.util.GetJsonUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.*;

@Controller
@CrossOrigin
@RequestMapping("manage")
public class TaskManageController {
    private int returnNum=-1;
    private String searchUsers="";
    @Autowired
    private TaskManageService taskManageService;

    Map<String,Object> map=new HashMap<String,Object>();

    /**
     * 查询出所有的用户工号和姓名
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/selectAllName",produces ={"application/json;charset=UTF-8"})
    public String selectAllName(){
        List<User> list=taskManageService.selectAllName();
        return GetJsonUtil.getUserString(list);
    }

    /**
     * 添加事务记录和用户id
     *
     * @return
     */

    @ResponseBody
    @RequestMapping(value = "/addTask",produces ={"application/json;charset=UTF-8"})
    public void addTask(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String title=request.getParameter("title");
        String taskName=request.getParameter("task");
        String type=request.getParameter("type");
        String reqValue=request.getParameter("request");
        String finishTime=request.getParameter("finish_time");

        String people=request.getParameter("people");
        String[] peopleIds=people.split("\\,");
        for (String peopleId : peopleIds) {
            System.out.println(peopleId);
        }

        Date date=new Date();
        String dateTime= DateFormatUtil.DateFormat(date);
        Task task=new Task(title,taskName,type,reqValue,dateTime,finishTime);
        int returnNum=taskManageService.add(task,dateTime,peopleIds);

        System.out.println("return1:"+returnNum);
        if(returnNum==0) {
            response.getWriter().write("<script>alert('添加成功');</script>");
            response.getWriter().write("<script>window.location.href = 'http://159.75.52.87/Counselor_System/work-look/work-look.html';</script>");
        }else{
            response.getWriter().write("<script>alert('添加失败');</script>");
            response.getWriter().write("<script>window.location.href = 'http://159.75.52.87/Counselor_System/work-add/work-add.html';</script>");
        }
    }

    /**
     * 查询所有事务
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/tasks",produces ={"application/json;charset=UTF-8"})
    public String selectAll(){
        List<Task> list=taskManageService.selectAll();
        for (Task task : list) {
            task.setCreateTime(DateFormatUtil.DateFormat(task.getCreate_time()));
            task.setFinishTime(DateFormatUtil.DateFormat(task.getFinish_time()));
        }
        return GetJsonUtil.getTaskString(list);
    }

    /**
     *
     * 根据 标题 发布时期 结束时期 动态返回事务列表
     * @return
     */
    @RequestMapping(value = "searchTaskByParam",produces = {"application/json;charset=UTF-8"})
    public @ResponseBody String searchTaskByParam(HttpServletRequest request) throws Exception {
        String title=request.getParameter("title");
        String create_time=request.getParameter("start");
        String finish_time=request.getParameter("end");
        System.out.println("成功");
        //成功
        List<Task> tasks=taskManageService.searchTaskByParam(title,create_time,finish_time);
        for (Task task : tasks) {
            task.setCreateTime(DateFormatUtil.DateFormat(task.getCreate_time()));
            task.setFinishTime(DateFormatUtil.DateFormat(task.getFinish_time()));
        }
        return GetJsonUtil.getTaskString(tasks);
    }

    /**
     * 根据会议编号查询拥有该会议的用户
     * @param id
     * @return
     */
    //@ResponseBody
    @RequestMapping(value = "/users",produces = {"application/json;charset=UTF-8"})
    public void selectUsersByID(@RequestParam int id){
        searchUsers="";
        //System.out.println("=========================================================");
        //System.out.println("会议编号是"+id);
        List<UserStatus> uus=taskManageService.selectUsersByID(id);
        //System.out.println("ut是"+uus);
        List<User> users=new ArrayList<User>();
        for (UserStatus uu : uus) {
            users.add(new User(uu.getId(),uu.getName(),uu.getStatus()));
        }
        //System.out.println("users是"+users);
        searchUsers= GetJsonUtil.getUserString(users);
    }
    @RequestMapping(value = "/returnSearchUsers",produces = {"application/json;charset=UTF-8"})
    @ResponseBody
    public String returnSearchUsers() throws InterruptedException {
        //System.out.println("最后返回信息："+searchUsers);
        return searchUsers;
    }


}
